//1.创建Node节点类Node
class Node<T> {
    value: T
    next: Node<T> | null = null 
    constructor(value: T) {
        this.value = value
    }
}

//2.创建LinkedList的类
class LinkList<T> {
    head: Node<T> | null = null
    private size: number = 0
    
    get length(){
        return this.size
    }

    //追加节点,只在最后的位置插入
    append(value:T){
        //1.根据value创建一个新节点
        const newNode = new Node(value)
        //2.判断this.head是否为null
        if(!this.head){
            this.head = newNode
        }else{
            let current = this.head;
            while(current.next){
                current = current.next
            }
            current.next = newNode
        }
        //3.size++
        this.size++
    }

    //遍历链表的方法
    traverse(){
        const values: T[] = []
        
        let current = this.head
        while(current){
            values.push(current.value)
            current = current.next
        }
        console.log(values.join('->'))
    }
}

const linkedList = new LinkList<string>()
linkedList.append('aaa')
linkedList.append('bbb')
linkedList.append('ccc')
linkedList.append('ddd')
linkedList.traverse()

export {}